package com.cxhome.cxinfox.cxmenu;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.traffic.managent.base.BaseService;
import com.traffic.managent.base.Paginator;
import com.traffic.managent.model.system.Module;
import com.traffic.managent.model.system.Operator;
import com.traffic.managent.util.SQLUtils;

/**
 * 数据字典service
 */
public class OperatorService extends BaseService {
	public static final OperatorService service = new OperatorService();
	
	
	public Page<Operator> paginate(Operator model, Paginator paginate) {
		SQLUtils sql = new SQLUtils(
				" from cx_operator o left join cx_module m on o.moduleids = m.ids left join cx_systems s on s.ids = m.systemsids where 1=1 ");
		if (model.getAttrValues().length > 0) {
			sql.setAlias("o");
			// 查询条件
			sql.whereLike("names", model.getStr("names"));
		}

		return Operator.dao.paginate(paginate,
				"select o.ids, o.names, o.url, o.rowFilter, o.splitPage, o.formToken, o.privilegess, m.names as modulenames, s.names as systemsnames ",
				sql.toString());

	}
	
	
	/**
	 * 获取根目录下拉框
	 * @param selected
	 * @return
	 */
	public String selectMoudle(String selected) {
		List<Module> list = Module.dao.findByWhere(" where 1 = 1  order by orderids ");
		StringBuffer sb = new StringBuffer();
		for (Module menu : list) {
			sb.append("<option value=\"");
			sb.append(menu.getStr("ids"));
			sb.append("\" ");
			if (selected != null) {
				sb.append(menu.getStr("ids") == selected ? "selected" : "");
			}
			sb.append(">");
			sb.append(menu.getStr("names"));
			sb.append("</option>");
		}
		return sb.toString();
	}

	/**
	 * 根据父节点获取List
	 * @param parentid
	 * @return
	 */
	public List<Operator> getListByParentid(int parentid) {
		List<Operator> list = Operator.dao.findByWhere(" where  status = 1 and parentid = ? order by sort ",
				parentid);
		return list;
	}

	/**
	 * 
	 * @param moduleids
	 * @return
	 */
	public String getAllOperators(String menuids) {
		String sql = "select operatorids from cx_menu where 1=1 and ids = ?";
		Record record = Db.findFirst(sql, menuids);
		String operatorids = record.getStr("operatorids");

		return operatorids;
	}
}
